package cn.lsh.writable;

import org.apache.hadoop.io.WritableComparable;

import java.io.DataInput;
import java.io.DataOutput;
import java.io.IOException;

public class Weather implements WritableComparable<Weather> {
	private int year;

	private int month;

	private int day;

	private int wd;

	public int getYear() {
		return year;
	}

	public void setYear(int year) {
		this.year = year;
	}

	public int getMonth() {
		return month;
	}

	public void setMonth(int month) {
		this.month = month;
	}

	public int getDay() {
		return day;
	}

	public void setDay(int day) {
		this.day = day;
	}

	public int getWd() {
		return wd;
	}

	public void setWd(int wd) {
		this.wd = wd;
	}

	@Override
	public int compareTo(Weather o) {
		//按日期正序排序
		int i = Integer.compare(this.year, o.getYear());
		if (i == 0) {
			i = Integer.compare(this.month, o.getMonth());
			if (i == 0) {
				return Integer.compare(this.day, o.getDay());
			}
			return i;
		}
		return i;
	}

	@Override
	public void write(DataOutput dataOutput) throws IOException {
		//序列化，传入一个输出流，读取对象
		dataOutput.writeInt(this.year);
		dataOutput.writeInt(this.month);
		dataOutput.writeInt(this.day);
		dataOutput.writeInt(this.wd);
	}

	@Override
	public void readFields(DataInput dataInput) throws IOException {
		//传入一个输入流，构建对象
		this.year = dataInput.readInt();
		this.month = dataInput.readInt();
		this.day = dataInput.readInt();
		this.wd = dataInput.readInt();
	}

	@Override
	public String toString() {
		return year + "-" + (month > 9 ? month : "0" + month) + "-" + (day > 9 ? day : "0" + day);
	}
}
